package com.zxx.otherchaos.widgets.rvScroll;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author zxx on 2021/4/14
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.BaseViewHolder> {

    private List<String> mList;
    private Context mContext;


    public MyAdapter(Context context, List<String> list) {
        this.mContext = context;
        mList = list;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LinearLayout layout = new LinearLayout(parent.getContext());
        layout.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        layout.setOrientation(LinearLayout.VERTICAL);

        AppCompatTextView textView = new AppCompatTextView(mContext);
        textView.setTextSize(30);
        textView.setBackgroundColor(0xffeeeeee);
        layout.addView(textView);

        RecyclerView recyclerView = new RecyclerView(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setNestedScrollingEnabled(true);
        recyclerView.setAdapter(new ItemAdapter(new ArrayList<String>()));

        layout.addView(recyclerView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        return new BaseViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        ViewGroup group = (ViewGroup) holder.itemView;
        TextView textView = (TextView) group.getChildAt(0);
        textView.setText(mList.get(position));

        RecyclerView recyclerView = (RecyclerView) group.getChildAt(1);
        ItemAdapter adapter = (ItemAdapter) recyclerView.getAdapter();
        adapter.mList.clear();
        for (int i = 0; i < 6; i++) {
            adapter.mList.add("item" + i);
        }

        adapter.notifyDataSetChanged();//在bind时确定好数据
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


    private class ItemAdapter extends RecyclerView.Adapter<BaseViewHolder> {

        private final List<String> mList;

        public ItemAdapter(List<String> list) {
            mList = list;
        }


        @NonNull
        @Override
        public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            AppCompatTextView textView = new AppCompatTextView(mContext);
            textView.setTextSize(30);
            textView.setBackgroundColor(0xffff00fe);
            textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            return new BaseViewHolder(textView);
        }

        @Override
        public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
            TextView textView = (TextView) holder.itemView;
            textView.setText(mList.get(position));

            if (position >= mList.size() - 2) {
                textView.getLayoutParams().height = 600;
            } else {
                textView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
            }

            textView.setLayoutParams(textView.getLayoutParams());
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }
}
